﻿using System;
using System.Windows.Forms;
using Database.Models;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using QuanLyThongTinCoSo.Forms.CanBoQuanLy;
using QuanLyThongTinCoSo.Forms.CapQuanLyHanhChinh;
using QuanLyThongTinCoSo.Forms.CoQuanQuanLyPCCC;
using QuanLyThongTinCoSo.Forms.DonVi;
using QuanLyThongTinCoSo.Forms.HinhThucDauTu;
using QuanLyThongTinCoSo.Forms.LoaiCoSo;
using QuanLyThongTinCoSo.Forms.LoaiHinhHoatDong;
using QuanLyThongTinCoSo.Forms.NganhOrLinhVuc;
using QuanLyThongTinCoSo.Forms.Phong;
using QuanLyThongTinCoSo.Forms.ThanhPhanKinhTe;

namespace QuanLyThongTinCoSo.Forms.PhieuDieuTraCoSo
{
    public partial class frmPhieuDieuTraCoSoDetail : frmObject
    {
        public frmPhieuDieuTraCoSoDetail()
        {
            InitializeComponent();

            //Load Data
            LoadPhong();
            LoadDoi();
            LoadDataCoQuanQlpccc();
            LoadDataCanBoQuanLy();
            LoadDataLoaiCoSo();
            LoadDataCapQuanLyHanhChinh();
            LoadDataHinhThucDauTu();
            LoadDataThanhPhanKinhTe();
            LoadDataLoaiHinhHoatDong();
            LoadDataNganh();
            LoadDataThamDuyetPCCC();
            LoadDataHangMuc();
            LoadPhuongTienPccc();
            LoadNguonNuoc();
            TuyenTruyenHuanLuyen();
            AnToan();
            XuLyViPham();
            //Set variable
            txtSo.Text = Constant.SoDetail.TenSo;
        }

        #region Cấp quản lý hành chính

        private void LoadDataCapQuanLyHanhChinh()
        {
            var biz = new CapQuanLyHanhChinhCoSoBiz();
            drlCapQuanLyHanhChinh.DataSource = biz.GetAll();
            drlCapQuanLyHanhChinh.DisplayMember = "Ten";
            drlCapQuanLyHanhChinh.ValueMember = "ID";
        }

        private void btnAddCapQuanLyHanhChinh_Click(object sender, EventArgs e)
        {
            var frm = new frmCapQuanLyHanhChinhDetail();
            frm.OnLoadData += LoadDataCapQuanLyHanhChinh;
            frm.ShowDialog();
        }
        #endregion

        #region Hình thức đầu tư

        private void LoadDataHinhThucDauTu()
        {
            var biz = new HinhThucDauTuCoSoBiz();
            drlHinhThucDauTu.DataSource = biz.GetAll();
            drlHinhThucDauTu.DisplayMember = "Ten";
            drlHinhThucDauTu.ValueMember = "ID";
        }

        private void btnAddHinhThucDauTu_Click(object sender, EventArgs e)
        {
            var frm = new frmHinhThucDauTuDetail();
            frm.OnLoadData += LoadDataHinhThucDauTu;
            frm.ShowDialog();
        }

        #endregion

        #region Phong
        private void LoadPhong()
        {
            var biz = new PhongBiz();
            drlPhong.DataSource = biz.GetAll();
            drlPhong.DisplayMember = "Ma";
            drlPhong.ValueMember = "ID";
        }

        private void btnPhong_Click(object sender, EventArgs e)
        {
            var frm = new frmPhongDetail();
            frm.OnLoadData += LoadPhong;
            frm.ShowDialog();
        }
        #endregion

        #region Doi
        private void LoadDoi()
        {
            var biz = new DoiBiz();
            drlDoi.DataSource = biz.GetAll();
            drlDoi.DisplayMember = "TenDoi";
            drlDoi.ValueMember = "ID";
        }

        private void btnAddDoi_Click(object sender, EventArgs e)
        {
            var frm = new frmDoiDetail();
            frm.OnLoadData += LoadDoi;
            frm.ShowDialog();
        }
        #endregion

        #region Cơ quan quản lý PCCC
        
        private void LoadDataCoQuanQlpccc()
        {
            var biz = new CoQuanQuanLyPCCCBiz();
            drlCoQuanQL.DataSource = biz.GetAll();
            drlCoQuanQL.DisplayMember = "Ten";
            drlCoQuanQL.ValueMember = "ID";
        }

        private void btnAddCoQuanQL_Click(object sender, EventArgs e)
        {
            var frm = new frmCoQuanQuanLyPCCCDetail();
            frm.OnLoadData += LoadDataCoQuanQlpccc;
            frm.ShowDialog();
        }

        #endregion

        #region Cán bộ quản lý
        private void LoadDataCanBoQuanLy()
        {
            var biz = new CanBoQuanLyBiz();
            drlCanBoQuanLy.DataSource = biz.GetAll();
            drlCanBoQuanLy.DisplayMember = "HoTen";
            drlCanBoQuanLy.ValueMember = "ID";
        }

        private void btnAddCanBoQuanLy_Click(object sender, EventArgs e)
        {
            var frm = new frmCanBoQuanLyDetail();
            frm.OnLoadData += LoadDataCanBoQuanLy;
            frm.ShowDialog();
        }
        #endregion

        #region Loại Cơ Sở
        private void LoadDataLoaiCoSo()
        {
            var biz = new LoaiCoSoBiz();
            drlLoaiCoSo.DataSource = biz.GetAll();
            drlLoaiCoSo.DisplayMember = "Ma";
            drlLoaiCoSo.ValueMember = "ID";
        }

        private void btnAddLoaiCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiCoSoDetail();
            frm.OnLoadData += LoadDataLoaiCoSo;
            frm.ShowDialog();
        }
        #endregion

        #region Thành phần kinh tế

        private void LoadDataThanhPhanKinhTe()
        {
            var biz = new ThanhPhanKinhTeBiz();
            drlThanhPhanKinhTe.DataSource = biz.GetAll();
            drlThanhPhanKinhTe.DisplayMember = "Ten";
            drlThanhPhanKinhTe.ValueMember = "ID";
        }

        private void btnAddThanhPhanKinhTe_Click(object sender, EventArgs e)
        {
            var frm = new frmThanhPhanKinhTeDetail();
            frm.OnLoadData += LoadDataThanhPhanKinhTe;
            frm.ShowDialog();
        }
        #endregion

        #region Loại hình hoạt động
        private void LoadDataLoaiHinhHoatDong()
        {
            var biz = new LoaiHinhHoatDongCoSoBiz();
            drlLoaiHinhHoatDong.DataSource = biz.GetAll();
            drlLoaiHinhHoatDong.DisplayMember = "Ten";
            drlLoaiHinhHoatDong.ValueMember = "ID";
        }

        private void btnAddLoaiHinhHoatDong_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiHinhHoatDongDetail();
            frm.OnLoadData += LoadDataLoaiHinhHoatDong;
            frm.ShowDialog();
        }
        #endregion

        #region Ngành lĩnh vực
        private void LoadDataNganh()
        {
            var biz = new NganhLinhVucCoSoBiz();
            drlNganh.DataSource = biz.GetAll();
            drlNganh.DisplayMember = "Ten";
            drlNganh.ValueMember = "ID";
        }
        private void btnAddNganh_Click(object sender, EventArgs e)
        {
            var frm = new frmNganhOrLinhVucCoSoDetail();
            frm.OnLoadData += LoadDataNganh;
            frm.ShowDialog();
        }
        #endregion

        #region Thẩm duyệt PCCC
        private void LoadDataThamDuyetPCCC()
        {
            var biz = new ThamDuyetPCCCBiz();
            grvThamDuyet.DataSource = biz.GetAll();
        }
        private void btnRefreshThamDuyet_Click(object sender, EventArgs e)
        {
            LoadDataThamDuyetPCCC();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmThamDuyetPCCC();
            frm.OnLoadData += LoadDataThamDuyetPCCC;
            frm.ShowDialog();
        }

        private void btnEditThamDuyet_Click(object sender, EventArgs e)
        {
            EditData();
        }

        private void EditData()
        {
            if (grvThamDuyet.SelectedRows.Count > 0)
            {
                var selectedRow = grvThamDuyet.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmThamDuyetPCCC(id))
                {
                    fEdit.OnLoadData += LoadDataThamDuyetPCCC;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void btnDelThamDuyet_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvThamDuyet.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvThamDuyet.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new ThamDuyetPCCC
                        {
                            Id = id
                        };

                        var biz = new ThamDuyetPCCCBiz();
                        biz.Delete(model);
                        LoadDataThamDuyetPCCC();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Hạng Mục
        private void btnAddHangMuc_Click(object sender, EventArgs e)
        {
            var frm = new frmHangMuc();
            frm.OnLoadData += LoadDataHangMuc;
            frm.ShowDialog();
        }

        private void btnEditHangMuc_Click(object sender, EventArgs e)
        {
            if (grvHangMuc.SelectedRows.Count > 0)
            {
                var selectedRow = grvHangMuc.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmHangMuc(id))
                {
                    fEdit.OnLoadData += LoadDataHangMuc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void LoadDataHangMuc()
        {
            var biz = new HangMucBiz();
            grvHangMuc.DataSource = biz.GetAll();
        }

        private void btnRefreshHangMuc_Click(object sender, EventArgs e)
        {
            LoadDataHangMuc();
        }

        private void btnXoaHangMuc_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvHangMuc.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvHangMuc.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new HangMuc()
                        {
                            Id = id
                        };

                        var biz = new HangMucBiz();
                        biz.Delete(model);
                        LoadDataHangMuc();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region PhuongTienPCCC
        private void LoadPhuongTienPccc()
        {
            var biz = new PhuongTienCoSoBiz();
            grvPhuongTien.DataSource = biz.GetAll();
        }

        private void btnAddPhuongTienPCCC_Click(object sender, EventArgs e)
        {
            var frm = new frmPhuongTienPCCC();
            frm.OnLoadData += LoadPhuongTienPccc;
            frm.ShowDialog();
        }

        private void btnEditPhuongTien_Click(object sender, EventArgs e)
        {
            if (grvPhuongTien.SelectedRows.Count > 0)
            {
                var selectedRow = grvPhuongTien.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmPhuongTienPCCC(id))
                {
                    fEdit.OnLoadData += LoadPhuongTienPccc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void btnRefreshPhuongTien_Click(object sender, EventArgs e)
        {
            LoadPhuongTienPccc();
        }

        private void btnDeletePhuongTien_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvPhuongTien.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvPhuongTien.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new PhuongTienCoSo()
                        {
                            Id = id
                        };

                        var biz = new PhuongTienCoSoBiz();
                        biz.Delete(model);
                        LoadPhuongTienPccc();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Nguồn nước
        private void LoadNguonNuoc()
        {
            var biz = new NguonNuocBiz();
            grvNguonNuoc.DataSource = biz.GetAll();
        }

        private void btnRefreshNguonNuoc_Click(object sender, EventArgs e)
        {
            LoadNguonNuoc();
        }

        private void btnAddNguonNuoc_Click(object sender, EventArgs e)
        {
            var frm = new frmNguonNuoc();
            frm.OnLoadData += LoadNguonNuoc;
            frm.ShowDialog();
        }

        private void btnEditNguonNuoc_Click(object sender, EventArgs e)
        {
            if (grvNguonNuoc.SelectedRows.Count > 0)
            {
                var selectedRow = grvNguonNuoc.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmNguonNuoc(id))
                {
                    fEdit.OnLoadData += LoadNguonNuoc;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void btnDelNguonNuoc_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvNguonNuoc.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvNguonNuoc.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new NguonNuoc
                        {
                            Id = id
                        };

                        var biz = new NguonNuocBiz();
                        biz.Delete(model);
                        LoadNguonNuoc();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Tuyên truyền huấn luyện
        private void TuyenTruyenHuanLuyen()
        {
            var biz = new TuyenTruyenHuanLuyenBiz();
            grvTuyenTruyenHuanLuyen.DataSource = biz.GetAll();
        }

        private void btnAddTuyenTruyenHuanLuyen_Click(object sender, EventArgs e)
        {
            var frm = new frmTuyenTruyenHuanLuyen();
            frm.OnLoadData += TuyenTruyenHuanLuyen;
            frm.ShowDialog();
        }

        private void btnRefreshTuyenTruyenHuanLuyen_Click(object sender, EventArgs e)
        {
            TuyenTruyenHuanLuyen();
        }

        private void btnEditTuyenTruyenHuanLuyen_Click(object sender, EventArgs e)
        {
            if (grvTuyenTruyenHuanLuyen.SelectedRows.Count > 0)
            {
                var selectedRow = grvTuyenTruyenHuanLuyen.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmTuyenTruyenHuanLuyen(id))
                {
                    fEdit.OnLoadData += TuyenTruyenHuanLuyen;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void btnDelTuyenTruyenHuanLuyen_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvTuyenTruyenHuanLuyen.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvTuyenTruyenHuanLuyen.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new TuyenTruyenHuanLuyen
                        {
                            Id = id
                        };

                        var biz = new TuyenTruyenHuanLuyenBiz();
                        biz.Delete(model);
                        TuyenTruyenHuanLuyen();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Kiểm tra, An Toàn
        private void btnAddAnToan_Click(object sender, EventArgs e)
        {
            var frm = new frmKiemTraAnToan();
            frm.OnLoadData += AnToan;
            frm.ShowDialog();
        }

        private void AnToan()
        {
            var biz = new KiemTraAnToanBiz();
            grvAnToan.DataSource = biz.GetAll();
        }

        private void btnRefreshAnToan_Click(object sender, EventArgs e)
        {
            AnToan();
        }

        private void btnEditAnToan_Click(object sender, EventArgs e)
        {
            if (grvAnToan.SelectedRows.Count > 0)
            {
                var selectedRow = grvAnToan.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmKiemTraAnToan(id))
                {
                    fEdit.OnLoadData += AnToan;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void btnDelAnToan_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvAnToan.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvAnToan.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new KiemTraAnToan()
                        {
                            Id = id
                        };

                        var biz = new KiemTraAnToanBiz();
                        biz.Delete(model);
                        AnToan();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Xu Ly Vi Pham
        private void btnAddXuLyViPham_Click(object sender, EventArgs e)
        {
            var frm = new frmXuLyViPham();
            frm.OnLoadData += XuLyViPham;
            frm.ShowDialog();
        }

        private void XuLyViPham()
        {
            var biz = new XuLyViPhamBiz();
            grvXuLyViPham.DataSource = biz.GetAll();
        }

        private void btnRefreshXuLyViPham_Click(object sender, EventArgs e)
        {
            XuLyViPham();
        }

        private void btnEditXuLyViPham_Click(object sender, EventArgs e)
        {
            if (grvXuLyViPham.SelectedRows.Count > 0)
            {
                var selectedRow = grvXuLyViPham.SelectedRows[0];
                var id = int.Parse(selectedRow.Cells[0].Value.ToString());
                using (var fEdit = new frmXuLyViPham(id))
                {
                    fEdit.OnLoadData += XuLyViPham;
                    fEdit.ShowDialog();
                }
            }
            else
            {
                AlertList.Error(Constant.Messages.UpdateSelectItemError);
            }
        }

        private void btnDelXuLyViPham_Click(object sender, EventArgs e)
        {
            try
            {
                if (grvXuLyViPham.SelectedRows.Count != 0)
                {
                    var id = int.Parse(grvXuLyViPham.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == AlertList.Question(Constant.Messages.DeleteConfirm))
                    {
                        var model = new XuLyViPham
                        {
                            Id = id
                        };

                        var biz = new XuLyViPhamBiz();
                        biz.Delete(model);
                        XuLyViPham();
                    }
                }
                else
                {
                    AlertList.Error(Constant.Messages.UpdateSelectItemError);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region Tinh hinh chay no
        private void NewTinhHinhChayNo(object sender, EventArgs e)
        {
            var frm = new frmTinhHinhChayNo();
            frm.OnLoadData += TinhHinhChayNo;
            frm.ShowDialog();
        }

        private void TinhHinhChayNo()
        {
            var biz = new XuLyViPhamBiz();
            grvXuLyViPham.DataSource = biz.GetAll();
        }

        private void RefreshTinhHinhChayNo(object sender, EventArgs e)
        {
            TinhHinhChayNo();
        }
        #endregion

        private void btnAddPhuongAnCC_Click(object sender, EventArgs e)
        {
            var frm = new frmPhuongAnCC();
            frm.ShowDialog();
        }

        private void btnAddTinhHinhChayNo_Click(object sender, EventArgs e)
        {
            var frm = new frmTinhHinhChayNo();
            frm.ShowDialog();
        }

        private void btnAddThayDoiCoSo_Click(object sender, EventArgs e)
        {
            var frm = new frmThayDoiCoSo();
            frm.ShowDialog();
        }

        private void frmPhieuDieuTraCoSoDetail_Load(object sender, EventArgs e)
        {

        }

        

        

        

        

        

        

        

        

        

       

        

        

        

        

    }
}